package com.kemai.designmode.singleton

import com.kemai.common.utils.AppUtils

/**

 *@description:懒汉式
 * @author: zhaoshanshan
 * @date: 2022/10/3 4:33 下午

 */
class LazySingleInstance {

    //方式一：
    companion object{//双重安全锁校验
        private var instance:LazySingleInstance? = null;
        fun getInstance():LazySingleInstance?{
           if(null==instance){
               synchronized(LazySingleInstance::class){
                   if(null==instance){
                       instance =  LazySingleInstance();
                   }
               }
           }
            return instance
        }
    }
    //方式二：
//    companion object{
//        val instance:LazySingleInstance?by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED){LazySingleInstance()}
//    }
    fun getLazySingleInstance(){
        val lazySingleInstance = getInstance()
        AppUtils.getInstance()?.showToast("懒汉式:${lazySingleInstance.toString()}")
    }
}